package org.project
import groovy.sql.Sql

import java.util.Random

import model.CrawlerInfo;
import model.LinkChecker
class ForagerController {

	def dataSource
	URL u = new URL("http://www.spsu.edu")
	LinkChecker link = new LinkChecker(u)
	def check
	int min
	int max

	def index(){
		

	}

	def home(){
		[check:check]
			
	}

	def login(String username,String password){



		if(username.equals("user") && password.equals("pass")){

			session.user = "admin"//person currently logged in

			redirect(action: 'home')
		}
		else{
			flash.message = "Error, username or password invalid"
			redirect(action: 'index')
		}
	}


	def logout(){
		
		session.user = null;
		flash.message = "Goodbye Admin"
		redirect(action: 'index')
		

	}
	def statusControl() {
		def goodLink = CrawlerInfo.getGood()
		def badLink = CrawlerInfo.getBad()
		def timeElapsed = CrawlerInfo.getTimeElapsed()
		def webPageProcessed = CrawlerInfo.getWebpageProcessed()
		def webPageProcessing = CrawlerInfo.getProccessingLink()
		render "Good Links: ${goodLink} &nbsp;&nbsp;&nbsp;&nbsp; Bad Links: ${badLink}<br/>Time Elapsed: ${timeElapsed} &nbsp;&nbsp;&nbsp;&nbsp; Web Page Processed: ${webPageProcessed}<br/>Processing: ${webPageProcessing}"

	}
	def extract = {

		def sql = new Sql(dataSource)
		
		int number = LinkChecker.isTable()

		def errors = sql.rows("SELECT *from table${number}")
		
		def address = "Page Address"
		def error   = "Error Type"
		def link    = "Page Link"


		render (template:'test', model:[errors:errors,address:address,error:error,link:link] )

	}
	def getLinkCrawler(){
		URL u = new URL("http://www.spsu.edu")

	}
	def stop(){

		if(CrawlerInfo.isCrawlerRunningStatus()==true)
			link.stopScan()
			
		CrawlerInfo.setCrawlerRunningStatus(false)
		check = true

	}
	
	def scan = {
		
		def sql = new Sql(dataSource)
		
		
		//def errors = sql.rows("SELECT *from table${params.id}")
		
		def test = params.id
		
		
		if(!(test.equals('0')))
		{
		
		def trial = "SELECT *from table"+params.id
		

		
		def errors = sql.rows(trial)
		
		def address = "Page Address"
		def error   = "Error Type"
		def link    = "Page Link"
		
		render (template:'test3', model:[errors:errors,address:address,error:error,link:link] )

		}
		
		
		
	}
	
	
	def archive = {
		
		
		stop()
		
		def sql = new Sql(dataSource)

		def results = sql.rows("SELECT *from tablenumber")
		
		def column1 = "Scan Number"
		def column2 = "Good Links"
		def column3 = "Bad Links"
		def column4 = "Status"
		def column5 = "Time Elapsed"
		def column6 = "Date"
		
		def num = sql.firstRow("SELECT *FROM tablenumber ORDER BY tables DESC")
		
		if(num==null)
			max = 0
		else
			max = num.tables
		
		def num2 = sql.firstRow("SELECT *FROM tablenumber")
		
		if(num2==null)
			min = 0
		else
			min = num2.tables

		
		render (template:'test2', model:[results:results, column1:column1 , column2:column2, column3:column3, column4:column4, column5:column5, column6:column6,min:min,max:max] )
		
	}
	

	def forage(){
		check = CrawlerInfo.isCrawlerRunningStatus()
			//checkStart = CrawlerInfo.isStartButtonStatus()
	
		if(CrawlerInfo.isCrawlerRunningStatus()==false){
			def sql = new Sql(dataSource)

			//sql.execute"drop table links"
			
			def num = sql.firstRow("SELECT *FROM tablenumber ORDER BY tables DESC")
			
			int number 
			
			if(num==null)
				number = 1
			else 
				number=num.tables+1
			
			sql.execute"create table table${number}(address varchar(400),error varchar(200),pageLink varchar(400))"
			
			

			//sql.execute"create table links(address varchar(100),error varchar(100),pageLink varchar(100))"
			//sql.execute"update tablenumber set tables=${number}"
			sql.execute"insert into tablenumber(tables) values(${number})"
			
			LinkChecker.setTable(number)

			link.begin()

			
		}
		render(view: 'home', model:[check:check])
	}
}
